var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sheight:null,
    indexTab:false,
    disabled: true,
    showPrivacy: false,
    privacyContractName:''
  },
  swiperbindchange(e){
    let that = this
    if(e.detail.current){
      this.setData({
        indexTab:true,
        disabled:true
      })
    }else{
      this.setData({
        indexTab:false,
        disabled:true
      })
    }
    wx.setStorageSync('tabNav', that.data.indexTab?'3':'1')
  },
  bindtapqh(e){
    let that = this
    that.setData({
      indexTab:!that.data.indexTab,
      disabled:true
    })
    wx.setStorageSync('tabNav', that.data.indexTab?'3':'1')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let winHeight = wx.getSystemInfoSync().windowHeight 
        that.setData({
            sheight:winHeight 
        })
    qqmapsdk = new QQMapWX({
      key: app.globalData.key
    })
    wx.getPrivacySetting({
      success: res => {
        if (res.needAuthorization) {
          // 需要弹出隐私协议
          this.setData({
            showPrivacy: true,
            privacyContractName:res.privacyContractName
          })
        } else {
          // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
        }
      },
      fail: () => {},
      complete: () => {}
    })
    if (wx.getStorageSync('token') && wx.getStorageSync('tabNav')) {
      if (wx.getStorageSync('tabNav') == 0) {
        return wx.redirectTo({
          url: '/pagesA/waiters/waiters',
        })
      } else if (wx.getStorageSync('tabNav') == 1) {
        return wx.redirectTo({
          url: '/pages/workbench/workbench',
        })
      }else{
        return wx.redirectTo({
          url: '/pagesB/index/index',
        })
      }
    }
  },
  bindtapSetting() {
    this.selectComponent('#dialogSetti').hide()
    wx.openSetting()
  },
  exitMiniProgram(){
    wx.exitMiniProgram()
  },
  handleAgreePrivacyAuthorization() {
    this.setData({
      showPrivacy:false
    })
  },
  handleOpenPrivacyContract() {
    wx.openPrivacyContract({
      success: () => {}, // 打开成功
      fail: () => {}, // 打开失败
      complete: () => {}
    })
  },
  onShow() {
    if(wx.getStorageSync('tabNav')==3){
      this.setData({
        indexTab:true
      })
    }else if(wx.getStorageSync('tabNav')==1){
      this.setData({
        indexTab:false
      })
    }
    if (wx.getStorageSync('token') && wx.getStorageSync('tabNav')) {
      if (wx.getStorageSync('tabNav') == 0) {
        return wx.redirectTo({
          url: '/pagesA/waiters/waiters',
        })
      } else if (wx.getStorageSync('tabNav') == 1){
        return wx.redirectTo({
          url: '/pages/workbench/workbench',
        })
      }
    }
  },
  toggle(e) {
    this.setData({
      disabled: !e.detail.checked
    })
  },
  bindtapLogin() {
    let that = this
    wx.showModal({
      title: '登录授权',
      content: '继续使用小程序将获取相关权限，如：手机号、蓝牙、地理位置等，手机号用作登录账号，地理位置为了获取当前所在地区医院治疗项目',
      success (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pagesA/loginI/loginI?tabNav=0',
          })
          wx.setStorageSync('ty', 1)
        } else if (res.cancel) {
          wx.getSystemInfo({
            success (resd) {
              let locationEnabled = resd.locationEnabled; //判断手机定位服务是否开启
              let locationAuthorized = resd.locationAuthorized; //判断定位服务是否允许微信授权
              if (locationEnabled == false) {
                that.showDialog({
                  title: '手机未打开定位功能',
                  icon: 'fail',
                  tip: ['']
                })
              }else if(locationAuthorized == false){
                that.showDialog({
                  title: '微信未授权定位权限',
                  icon: 'fail',
                  tip: ['请前往设置-应用内打开','微信定位信息权限']
                })
              }
              else {
                wx.getSetting({
                  success: res => {
                    console.log(res);
                    let authSetting = res.authSetting
                    if (authSetting['scope.userLocation']) {
                      if (!wx.getStorageSync('addresconter')) {
                        wx.setStorageSync('tabNav', 0)
                        wx.getLocation({
                          type: 'gcj02',
                          success(res) {
                            const latitude = res.latitude
                            const longitude = res.longitude
                            console.log(res, '微信获取经纬度');
                            qqmapsdk.reverseGeocoder({
                              location: {
                                latitude: latitude,
                                longitude: longitude
                              },
                              success: (res => {
                                let resultData = res.result.address_component
                                console.log(res.result.address_component, '地图调用获取位置信息');
                                let addresconter = resultData.province + ' ' + resultData.city + ' ' + resultData.district
                                wx.setStorageSync('addresconter', addresconter)
                                let address = {
                                  province: resultData.province,
                                  city: resultData.city,
                                  areas: resultData.district
                                }
                                wx.setStorageSync('address', address)
                                
                                wx.redirectTo({
                                  url: '/pagesA/waiters/waiters',
                                })
                              })
                            })
                          }
                        })
                      } else {
                        wx.setStorageSync('tabNav', 0)
                        wx.redirectTo({
                          url: '/pagesA/waiters/waiters',
                        })
                      }
                    } else {
                      if (authSetting['scope.userLocation'] == undefined) {
                        wx.setStorageSync('tabNav', 0)
                        if (!wx.getStorageSync('addresconter')) {
                          wx.getLocation({
                            type: 'gcj02',
                            success(res) {
                              const latitude = res.latitude
                              const longitude = res.longitude
                              console.log(res, '微信获取经纬度');
                              qqmapsdk.reverseGeocoder({
                                location: {
                                  latitude: latitude,
                                  longitude: longitude
                                },
                                success: (res => {
                                  let resultData = res.result.address_component
                                  console.log(res.result.address_component, '地图调用获取位置信息');
                                  let addresconter = resultData.province + ' ' + resultData.city + ' ' + resultData.district
                                  wx.setStorageSync('addresconter', addresconter)
                                  let address = {
                                    province: resultData.province,
                                    city: resultData.city,
                                    areas: resultData.district
                                  }
                                  wx.setStorageSync('address', address)
                                  wx.redirectTo({
                                    url: '/pagesA/waiters/waiters',
                                  })
                                })
                              })
                            }
                          })
                        } else {
                          wx.redirectTo({
                            url: '/pagesA/waiters/waiters',
                          })
                        }
                      } else {
                        that.selectComponent('#dialogSetti').show()
                      }
                    }
                  }
                })
              }
            },
            fail(){
              that.showDialog({
                title: '手机未打开定位功能',
                icon: 'fail',
                tip: ['']
              })
            }
          })
          
        }
      }
    })
    
    // wx.showModal({
    //   title: '提示',
    //   content: '小程序需要使用手机号登录，是否继续？',
    //   success(res) {
    //     if (res.confirm) {
    //       wx.navigateTo({
    //         url: '/pagesA/loginI/loginI?tabNav=0',
    //       })
    //     } else if (res.cancel) {
    //       console.log('用户点击取消')
    //     }
    //   }
    // })

  },
  bindtapLoginInstitution() {
    if(wx.getStorageSync('token1')){
      wx.setStorageSync('token', wx.getStorageSync('token1'))
      wx.reLaunch({
        url: '/pages/workbench/workbench',
      })
    }else{
      wx.navigateTo({
        url: '/pagesA/loginI/loginI?tabNav=1',
      })
    }
    // wx.showModal({
    //   title: '提示',
    //   content: '小程序需要使用手机号登录，是否继续？',
    //   success(res) {
    //     if (res.confirm) {
    //       wx.navigateTo({
    //         url: '/pagesA/loginI/loginI?tabNav=1',
    //       })
    //     } else if (res.cancel) {
    //       console.log('用户点击取消')
    //     }
    //   }
    // })
  },
  bindtapLoginjjg(){
    if(wx.getStorageSync('token3')){
      wx.setStorageSync('token', wx.getStorageSync('token3'))
      wx.reLaunch({
        url: '/pagesB/index/index',
      })
    }else{
      wx.navigateTo({
        url: '/pagesA/loginI/loginI?tabNav=3',
      })
    }
  }
})